﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Homework_4
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            //初始化可选择文件
            GetReadFile(comboBox1);
            GetReadFile(comboBox2);
            GetReadFile(comboBox3);
            GetWriteFile(comboBox3);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            button1.Text = FileCombine.CombineFile();
            GetWriteFile(comboBox3);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            button1.Text = FileCombine.ShowFile();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            ComboBox combo = (ComboBox)sender;
            FileCombine.readFile1 = combo.SelectedItem.ToString();
        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            ComboBox combo = (ComboBox)sender;
            FileCombine.readFile2 = combo.SelectedItem.ToString();
        }

        private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
        { 
            ComboBox combo = (ComboBox)sender;
            FileCombine.showFile = combo.SelectedItem.ToString();
        }

        //选择文件函数
        public void GetReadFile(ComboBox comboBox)
        {
            string[] files = System.IO.Directory.GetFiles(@"../../source/");                  
            foreach (string file in files)
            {
                comboBox.Items.Add(file);
            }
        }

        public void GetWriteFile(ComboBox comboBox)
        {
            string dataDir = "../../Data/";
            if (System.IO.Directory.Exists(@dataDir))
            {
                string[] files = System.IO.Directory.GetFiles(dataDir);
                foreach (string file in files)
                {
                    if (!comboBox.Items.Contains(file))
                    {
                        comboBox.Items.Add(file);
                    }
                }
            } 
        }

        
        //写文件的类
        public static class FileCombine
        {
            public static string readFile1="", readFile2="", writeFile="", showFile="";
            //写文件
            public static string CombineFile() 
            {
                string retStr="";
                if (readFile1!=""&&readFile2!="")
                {
                    try
                    {
                        //创建新路径Data并创建新文件newtext.txt
                        System.IO.Directory.CreateDirectory(@"../../Data");
                        string path = System.IO.Path.Combine("../../Data", "newtext.txt");
                        //使用FileStream将选中的读文件的内容写入newtwxt.txt
                        System.IO.FileStream fs = System.IO.File.Create(path);
                        byte[] buffer = System.IO.File.ReadAllBytes(readFile1);
                        fs.Write(buffer, 0, buffer.Length);
                        buffer = System.IO.File.ReadAllBytes(readFile2);
                        fs.Write(buffer, 0, buffer.Length);
                        fs.Close();
                        retStr = "合并成功！";
                    }
                    catch (Exception e)
                    {
                        retStr = e.Message;
                    }  
                }
                else
                {
                    retStr = "请先选择要合并的文件！"; 
                }
                return retStr;
            }
            //输出文件
            public static string ShowFile()
            {
                string showOnSr = "";
                if (showFile!="")
                {
                    try
                    {
                        showOnSr = "文件内容:\n" + System.IO.File.ReadAllText(showFile);
                    }
                    catch (Exception e)
                    {
                        showOnSr = e.Message;
                    }
                }
                else
                {
                    showOnSr = "请先选中要读取的文件!";
                }
                return showOnSr;
            }
        }
    }
}
